//
//  LGBasicCell.m
//  微博
//
//  Created by MS on 15-12-27.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGBasicCell.h"
#import "LGBadgeView.h"
#import "BasicSetting.h"
@interface LGBasicCell ()

@property (nonatomic,strong) UIImageView * arrowView;
@property (nonatomic,strong) UIImageView * checkView;
@property (nonatomic,strong) LGBadgeView * badgeView;
@property (nonatomic,strong) UISwitch * swichView;
@property (nonatomic,strong) UILabel * labelView;
@end

@implementation LGBasicCell

-(UIImageView *)arrowView
{
    if (_arrowView==nil) {
        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_arrow"]];
    }
    return _arrowView;
}
-(UIImageView *)checkView
{
    if (_checkView==nil) {
        _checkView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_checkmark"]];
    }
    return _checkView;
}

-(LGBadgeView *)badgeView
{
    if (_badgeView==nil) {
        _badgeView = [LGBadgeView buttonWithType:UIButtonTypeCustom];
    }
    return _badgeView;
}

-(UISwitch *)swichView
{
    if (_swichView==nil) {
        _swichView = [[UISwitch alloc] init];
    }
    return _swichView;
}
-(UILabel *)labelView
{
    if (_labelView==nil) {
        _labelView = [[UILabel alloc] init];
        _labelView.textAlignment = NSTextAlignmentCenter;
        _labelView.textColor = [UIColor redColor];
        [self addSubview:_labelView];
    }
    return _labelView;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.detailTextLabel.font = [UIFont systemFontOfSize:14];
        
        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageResizingWithName:@"common_card_middle_background"]];

        self.selectedBackgroundView =[[UIImageView alloc] initWithImage:[UIImage imageResizingWithName:@"common_card_middle_background_highlighted"]];
        self.backgroundColor = [UIColor clearColor];
    }
    return self ;
}

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * ID = @"cell";
    
    LGBasicCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell==nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    return cell;
}

-(void)setItem:(LGItem *)item
{
    _item = item;
    
    //设置内容
    [self setUpContent];
    
    //设置右边图
    [self setUpRightView];
}
//设置内容
-(void)setUpContent
{
    self.imageView.image = _item.image;
    self.textLabel.text = _item.title;
    self.detailTextLabel.text = _item.subTitle;
    
}
//设置右边图
-(void)setUpRightView
{
    if ([_item isKindOfClass:[LGArrowItem class]]) {
        
        self.accessoryView = self.arrowView;
    }
    else if([_item isKindOfClass:[LGSwitchItem class]]){
        self.accessoryView = self.swichView;
        LGSwitchItem * switchItem = (LGSwitchItem *)_item;
        self.swichView.on = switchItem.on;
        
    }
    else if([_item isKindOfClass:[LGCheckItem class]]){
        LGCheckItem * checkItem = (LGCheckItem *)_item;
        if (checkItem.cheak) {
             self.accessoryView = self.checkView;
        }
        else{
            self.accessoryView = nil;
        }

    }
    else if([_item isKindOfClass:[LGBadgeItem class]]){
        LGBadgeItem * badgeItem = (LGBadgeItem *)_item;
        self.accessoryView = self.badgeView;
        self.badgeView.badgeValue = badgeItem.badgeValue;

    }
    else if([_item isKindOfClass:[LGLabelItem class]]){
        
        LGLabelItem * labelItem = (LGLabelItem *)_item;
        self.labelView.text = labelItem.text;
    }

    
    else {
        
        self.accessoryView = nil;
        [_labelView removeFromSuperview];
        _labelView =nil;
    }
    
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.labelView.frame = self.bounds;
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
